package com.shangke.springbootkkyp.dao.sqlProvidr;

import com.shangke.springbootkkyp.pojo.Admin;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class AdminSqlProvider {
    /**
     * 新增地址
     * @param admin
     * @return
     */
    public String save(Admin admin) {
//        return "INSERT INTO `kkyp`.`admin`(`area`, `city`, `detailaddress`, `priority`, `province`, `tel`, `username`, `xxl_sso_sessionid`) \n" +
//                "VALUES ('江南', '杭州市', '下沙','0', '浙江省', '17379839318', '方慧平', '测试')";
        return "INSERT INTO `kkyp`.`admin`(`area`, `city`, `detailaddress`, `priority`, `province`, `tel`, `username`, `xxl_sso_sessionid`) " +
                "VALUES ('"+admin.getArea()+"', '"+admin.getCity()+"', '"+admin.getDetailaddress()+"','"+admin.getPriority()+"', '" +
                admin.getProvince()+"', '"+ admin.getTel()+"', '"+admin.getUsername()+"', '"+ admin.getXxlSsoSessionid()+"')";
    }

    /**
     * 按照id删除收货地址
     * @param id
     * @return
     */
    public String deleteById(Long id,String xxlSsoSessionid) {
        return "delete from admin where id=" + id+"and xxl_sso_sessionid="+xxlSsoSessionid;
    }

    /**
     * 修改收货地址，通过id修改
     */
    public String updaterById(Long id, HashMap<String, String> hashMap,String xxlSsoSessionid) {
        return new SQL() {{
            UPDATE("admin");
            Iterator iter = hashMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                SET(entry.getKey()+"="+entry.getValue());
            }
            WHERE("id="+id+"and xxl_sso_sessionid="+xxlSsoSessionid);

//            SET(
//                    WHERE(iter.hasNext()) {
//                         Map.Entry entry = (Map.Entry) iter.next();
//                         entry.getKey() = entry.getValue());
//                    }
//            );

        }}.toString();
    }

    /**
     * 按照id和授权编码
     * 查找收货地址详情
     * @return
     */
    public String findByIdAndXxlSsoSessionid(Long id, String xxlSsoSessionid) {
        return "select * from admin where id="+id+"and xxl_sso_sessionid="+xxlSsoSessionid;
    }

    /**
     * 设置默认地址 || 设置为不是默认地址
     * @param id
     * @param xxlSsoSessionid
     * @return
     */
    public String updateByIdAndXxlSsoSessionid(Long id, String xxlSsoSessionid, Integer defaultId) {
        return "update `admin` set `priority`="+defaultId+
                "where id="+id+"and xxl_sso_sessionid="+xxlSsoSessionid;
    }

    /**
     * 利用授权码直接查询默认地址，默认地址后为0
     */
    public String findByXxlSsoSessionid(String xxlSsoSessionid) {
        return "select * from admin where xxl_sso_sessionid="+xxlSsoSessionid+"priority="+1;
    }

    /**
     * 查询是否只有1个默认地址
     * @param xxlSsoSessionid
     * @return
     */
    public String findByXxlSsoSessionidAndDefaultId(String xxlSsoSessionid, Integer defaultId) {
        return "SELECT COUNT(*) FROM admin WHERE priority = "+defaultId+"xxl_sso_sessionid="+xxlSsoSessionid;
    }

    public String findByAdminIdAndXxlSsoSessionid(String adminId,String xxlSsoSessionid){
        return "select * from admin where priority="+adminId+"and xxl_sso_sessionid="+xxlSsoSessionid;
    }

    /**
     * 获取收货地址列表
     */
    public String find() {
        return "select * from admin ";
    }

    /**
     *  获得新增数据id
     */
    public String findMaxId(){
        return "select MAX(admin.id) FROM admin";
    }

}
